/**
 * 基础配置-单位配置表
 * @author xujie
 * @since 2021-03-01 09:35:57
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit'], function (exports) {

    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let table = layui.table;
    let laypage = layui.laypage;
    let layedit = layui.layedit;

    form.render(null, "zmn-form");

    $(document).on('click', '#zmn-identity-info-form-cancel', function () {
        closeTab();
    });

    function closeTab() {
        //parent.layui.admin.events.closeThisTabs();
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    }

    // 提交
    form.on('submit(zmn-form-submit)', function(obj) {

        let data = obj.field;
        let url = "/unit/addEdit.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(result) {
                if(result.status === 200) {
                    layer.msg(result.message, {"icon": 1});
                    closeTab();
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
            }
        })
        return false;
    });

    form.verify({
        positive: function (value) {
            if (value !== null && value !== '') {
                let pattern = /^\+?[0-9][0-9]*$/;
                if (!pattern.test(value) || parseInt(value) < 0 || parseInt(value) > 2147483647) {
                    return "请输入0~2147483647的正整数";
                }
            }
        },
        nameLength: function (value) {
            if (value.length > 10) {
                return "名称长度不能大于10";
            }
        }
    });

    //对外暴露的接口
    exports('plat_unit_detail', {});
});
